/**
 * 报表service
 */
(function(window, angular, undefined) {
	/**
	 * 报表service
	 */
	var OnlineMonitorDataReportService = angular.module(
			'OnlineMonitorDataReportService', [ 'ui.router','waterCommon' ]);

	OnlineMonitorDataReportService.service('$ReportService', function($waterDataCommon, $httpData, $state,$queryObj) {
		this.setSelectedTab = function(tabName) {
			$("#allreport ul.idTabs a[ui-sref]").removeClass("selected");
			$("#allreport ul.idTabs a[ui-sref='" + tabName + "']").addClass(
					"selected");
		}
		
		this.setSelectedTabSecond = function(tabName){
			$("#ReportSecond a").removeClass("selected");
			$("#" + tabName).addClass("selected");
		}
	});
	
	OnlineMonitorDataReportService.service('$DataReportService', function( $waterDataCommon,$rootScope,$sce) {
		
		this.showChar_for_new = function(data,domid,parmsToChar,pollInfo,dataObject,waterType){
			var parmMap = {};
			var parmToCheckList = parmsToChar;
			for(var i=0;i< parmToCheckList.length;i++){
				var obj = parmToCheckList[i];
				parmMap[obj.name] = obj.parmName;
			}
			
			$("#"+domid).empty();
			
			//获取全部历史数据用于图表展示
			if(data && data.length){
				for(var parm in parmMap){
					if(!parm){
						continue;
					}
					var currentParm = parm.toUpperCase();
					var currentCharData = [];					
					var parmNames = [parmMap[parm]];
					var datetimes = [];
					var charData = [];
					var charDiv="";
					if(waterType!=null && (waterType=='2' || waterType=='4')) {
						charDiv = $("<div class='charDiv' id='"+domid+parm+"'></div>").css({height: 300,width:$rootScope.Map_W});
					} else {
						charDiv = $("<div class='charDiv' id='"+domid+parm+"'></div>").css({height: 300,width:(data.length*150)});
					}
					$("#"+domid).append(charDiv);
					//获取污染物上下限,由于污染物名称大小写的问题，需要转换
					var parmData = pollInfo[currentParm];
					if(parmData) {
						var up = parmData.up;
						var down = parmData.down;
					} else {
						parmData = pollInfo[currentParm.toLowerCase()];
						var up = parmData.up;
						var down = parmData.down;
					}
					
					for(var i=0;i<data.length;i++){
						if(datetimes.length < data.length){
							var dateStr = data[data.length-1-i]["DATATIME"].split("月")[0];
							datetimes.push(dateStr);
						}
						
						var dataItem = data[data.length-1-i];
						var value = dataItem["RTD_"+parm];
						if(!value){
							value = dataItem["AVG_"+parm];
						}
						if(!value){
							value = dataItem["COU_"+parm];
						}
						if(!value){
							value = undefined;
						}
						currentCharData.push(value);
					}
					charData.push(currentCharData);
					$waterDataCommon.showMulitLineChar(parmNames,datetimes,charData,domid+parm,null,null,dataObject,down,up);
				}
			}
		}
		
		this.showChar_for_new_refacto = function(data,domid,parmsToChar,pollInfo,dataObject){
			var parmMap = {};
			var parmToCheckList = parmsToChar;
			for(var i=0;i< parmToCheckList.length;i++){
				var obj = parmToCheckList[i];
				parmMap[obj.name] = obj.parmName;
			}
			
			$("#"+domid).empty();
			
			//获取全部历史数据用于图表展示
			if(data && data.length){
				//for(var parm in parmMap){
				for(var i =0;i<pollInfo.length;i++){
					var parmData = pollInfo[i];
					var parm = parmData.name;
					var currentParm = parm.toUpperCase();
					var currentCharData = [];					
					var parmNames = [parmMap[parm]];
					var datetimes = [];
					var charData = [];
					var charDiv = $("<div class='charDiv' id='"+domid+parm+"'></div>").css({height: 300,width:($rootScope.Map_W - $rootScope.widthOff+200)/2});
					$("#"+domid).append(charDiv);
										
					if(parmData) {
						var up = parmData.up;
						var down = parmData.down;
					} else {
						parmData = pollInfo[currentParm.toLowerCase()]
						var up = parmData.up;
						var down = parmData.down;
					}
					
					for(var i=0;i<data.length;i++){
						if(datetimes.length < data.length){
							var dateStr = data[data.length-1-i]["DATATIME"].split("月")[0];
							datetimes.push(dateStr);
						}
						
						var dataItem = data[data.length-1-i];
						var value = dataItem["RTD_"+parm];
						if(!value){
							value = dataItem["AVG_"+parm];
						}
						if(!value){
							value = dataItem["COU_"+parm];
						}
						if(!value){
							value = undefined;
						}
						currentCharData.push(value);
					}
					charData.push(currentCharData);
					$waterDataCommon.showMulitLineChar(parmNames,datetimes,charData,domid+parm,null,null,dataObject,down,up);
				}
			}
		}
		
		this.showChar = function(data,domid,parmsToChar){
			var parmMap = {};
			var parmToCheckList = parmsToChar;
			for(var i=0;i< parmToCheckList.length;i++){
				var obj = parmToCheckList[i];
				parmMap[obj.name] = obj.parmName;
			}
			
			$("#"+domid).empty();
			
			//获取全部历史数据用于图表展示
			if(data && data.length){
				for(var parm in parmMap){
					if(!parm){
						continue;
					}
					var currentParm = parm;
					var currentCharData = [];					
					var parmNames = [parmMap[parm]];
					var datetimes = [];
					var charData = [];
					var charDiv = $("<div class='charDiv' id='"+domid+parm+"'></div>").css({height: 300,width:($rootScope.Map_W - $rootScope.widthOff+200)/2});
					$("#"+domid).append(charDiv);
					
					for(var i=0;i<data.length;i++){
						if(datetimes.length < data.length){
							var dateStr = data[data.length-1-i]["DATATIME"].split("月")[0];
							datetimes.push(dateStr);
						}
						
						var dataItem = data[data.length-1-i];
						var value = dataItem["P"+parm+"AVG"];
						if(!value){
							value = dataItem["P"+parm];
						}
						if(!value){
							value = dataItem["P"+parm+"COU"];
						}
						if(!value){
							value = undefined;
						}
						currentCharData.push(value);
					}
					charData.push(currentCharData);
					$waterDataCommon.showMulitLineChar(parmNames,datetimes,charData,domid+parm);
				}
			}
		}
		
		/**
		 * 获取污染物的各个阈值
		 */
		this.getParmLimit = function(parmToCheckList,limitData){
			if(parmToCheckList.length<1){
				return;
			}
		  var limitList=[];
		  
		  for(var i=0;i<parmToCheckList.length;i++){
				var pollCode=parmToCheckList[i].parmName;
				var down = limitData['DOWNP'+pollCode+"AVG"];
				var up = limitData['UPP'+pollCode+"AVG"];
				var unit = parmToCheckList[i].unit
				if(getWaterType()=='0'&&parmToCheckList[i].parmName=='B01'){
					unit="升/秒";
				}
				if(getWaterType()=='0'&&parmToCheckList[i].parmName=='B01TOTAL'){
					unit="吨";
				}
				var limitItem = "";
				if(!down && !up){
					limitItem = "";
				}
				else if(down && up){
					limitItem = down+"~"+up;
				}
				else if(!down){
					limitItem = "<"+up;
				}
				else if(!up){
					limitItem = ">"+down;
				}
				limitList.push(limitItem+"["+unit+"]");
			}
		  return limitList;
		}
		
		/**
		 * 获取报表表头
		 * dataSource 5：消减量
		 */
		this.getParmCheckList = function(parmToCheckList,dataSource){
        	var checkList=[];
        	//5表示消减量
        	if(dataSource == '5'){
        		for(var i=0;i<parmToCheckList.length;i++){
    				if($wdp.viewValue.wwtpReportParm.parmToCheckList[i].name == "B01"){
    					continue;
    				}
    				if($wdp.viewValue.wwtpReportParm.parmToCheckList[i].name == "001"){
    					continue;
    				}
    				var parmItem = $wdp.viewValue.wwtpReportParm.parmToCheckList[i];
    				checkList.push({
    					"headText":$sce.trustAsHtml(parmItem.text+'消减量'),
    					"parmName":parmItem.name.toUpperCase(),
    					"attrName":"P"+parmItem.name.toUpperCase()+"DENS"
    				});
    				
    				checkList.push({
    					"headText": $sce.trustAsHtml(parmItem.text + "消减率(%)"),
    					"parmName":parmItem.name.toUpperCase(),
    					"attrName":"P"+parmItem.name.toUpperCase()+"PRE"
    				});
    			}
        		//视为海口永庄实时数据参数控制
        	}else if(dataSource == 'real'){
        		for(var i=0;i<parmToCheckList.length;i++){
	        		var name=parmToCheckList[i].name;
	        		if(name== "011"||name== "065"||name== "060"||name== "101"||name== "PHE"){
						continue;
					}
	        		var parmItem =parmToCheckList[i];
	        		checkList.push({
    					"unit":parmItem.unit,
    					"parmName_text":parmItem.parmName,
    					"headText":parmItem.text,
    					"parmName":parmItem.name.toUpperCase(),
    					"parmName_original":parmItem.name_original, //给修约用，库里的代码是区分大小写的
    					"attrName":"P"+parmItem.name.toUpperCase()+"AVG"
    				});
        		}
        	}
        	else{
        		for(var i=0;i<parmToCheckList.length;i++){
        			var parmItem = parmToCheckList[i];		
                		/*if(parmItem.name == "B01" ){
        					continue;
        				}*/        				
        				checkList.push({
        					"unit":parmItem.unit,
        					"parmName_text":parmItem.parmName,
        					"headText":parmItem.text,
        					"parmName":parmItem.name.toUpperCase(),
        					"parmName_original":parmItem.name_original, //给修约用，库里的代码是区分大小写的
        					"attrName":"P"+parmItem.name.toUpperCase()+"AVG"
        				});       				
        				if(parmItem.name == "B01TOTAL" || parmItem.name == "001" || parmItem.name == "PHTEM"){
        					continue;
        				}       							
    			}
        	}
        	
        	return checkList;
		};
		/**
		 * 获取报表表头
		 * 通量信息
		 */
		this.getParmCheckCouList = function(parmToCheckList,dataSource){
        	var checkList=[];
        		for(var i=0;i<parmToCheckList.length;i++){
            		var parmItem = parmToCheckList[i];
            		if(this.ParameterFilter(parmItem.parmName)!=false){
	            		if(parmItem.name == "B01" ){
	    					continue;
	    				}
	    				
	            		if(parmItem.name == "B01TOTAL" || parmItem.name == "PHTEM" || parmItem.name == "001"|| parmItem.name == "B01"
	            			|| parmItem.name == "YD"|| parmItem.name == "COND"|| parmItem.name == "WATER_DEPTH"|| parmItem.name == "WATER_SPEED"){
	    					continue;
	    				} 
	            		
	    				checkList.push({
	    					"unit":parmItem.couunit	,
	    					"parmName_text":parmItem.parmName,
	    					"headText":parmItem.text,
	    					"parmName":parmItem.name.toUpperCase(),
	    					"parmName_original":parmItem.name_original, //给修约用，库里的代码是区分大小写的
	    					"attrName":"P"+parmItem.name.toUpperCase()+"COU"
	    				});
	        		}
        		}
        	return checkList;
       		
		};
		//通量参数过滤器
		this.ParameterFilter=function(ParameterName){
			var parame=["流速","水深","电导率","pH","累计流量","水温","风速"];
			for(var i=0;i<parame.length;i++){
				if(ParameterName==parame[i]){
					return false;
				}
			}
		}
		this.getYearWeek = function(year) {
			var weeks = [];
			var weeksum = 0;
			for(var month=1;month<=12;month++){
				var today = new Date();//今天
				
				var t1 = 0;//
				var t2 = 0;
				
		    	var currentDate = new Date();
		    	//周一为每星期第一天
		    	currentDate.setFullYear(year, month-1, 1);
		    	var w1 = currentDate.getDay();
		    	if (w1 == 0) {
		    	  w1 = 7;
		    	}
		    	
		    	//每月天数
		    	currentDate.setFullYear(year, month, 0);
		    	var dd = currentDate.getDate();
		    	// first Monday 第一个周一
		    	if (w1 != 1) {
		    		d1 = 7 - w1 + 2;
		    		if(month == 1){
		    			weeksum++; //如果一月一号不是星期一，则一月一号算是第一周的范围
		    		}
		    	} else {
		    		d1 = 1;
		    	}
		    	
		    	//本月有多少周
		    	week_count = Math.ceil((dd-d1+1)/7);
		    	
		    	for (var i = 0; i < week_count; i++) {
		    		weeksum++;
		    		var monday = d1+i*7;
		    		var sunday = monday + 6;
		    		var from = month+"月"+monday +"日";
		    		var start = "";
		    		if(month<10){
		    			start = year+"-0"+month+"-"+monday;
		    		}
		    		else{
		    			 start = year+"-"+month+"-"+monday;
		    		}
		    		
		    		t1 = currentDate.setFullYear(year,month-1,monday);
		    		
		    		var to;
		    		var end;
		    		if (sunday <= dd) {
		    			to = month+"月"+sunday+"日";
		    			if(month<10){
		    				end=year+"-0"+month+"-"+sunday;
			    		}
			    		else{
			    			end=year+"-"+month+"-"+sunday;
			    		}
		    			t2 = currentDate.setFullYear(year,month-1,sunday);
		    		} else {
		    			currentDate.setFullYear(year, month-1, sunday);
		    			to = (currentDate.getMonth()+1)+"月"+currentDate.getDate()+"日";
		    			if((currentDate.getMonth()+1)<10){
		    				end=currentDate.getFullYear()+"-0"+(currentDate.getMonth()+1)+"-"+currentDate.getDate();
		    			}
		    			else{
		    				end=currentDate.getFullYear()+"-"+(currentDate.getMonth()+1)+"-"+currentDate.getDate();
		    			}
		    			t2 = currentDate.setFullYear(year, month-1, sunday+1);
		    		}
		    		
		    		var obj=new Object();
		    		obj.name="第"+(weeksum)+"周 " + from + " 到 " + to;
		    		obj.value=start+","+end+","+weeksum;
		    		obj.today = false;
		    		if(today.getTime()>=t1 && today.getTime()<t2){
		    			obj.today = true;
		    		}
		    		weeks.push(obj);
		    	}
			}
			return weeks;
	    };
	});
	
	OnlineMonitorDataReportService.factory('XZQSearchService', [ function() {
		var services = {};

		var selectedXZQ = undefined;
		var selectedWRW = undefined;
		var changedCallBack = undefined;

		services.setChangedCallBack = function(callBack) {
			if(angular.isFunction(callBack)){
				changedCallBack = callBack;
				changedCallBack();
			}
		};
		services.getChangedCallBack = function() {
			return changedCallBack;
		};

		services.setXZQ = function(xzq) {
			selectedXZQ = xzq;
		};
		services.getXZQ = function() {
			return selectedXZQ;
		};

		services.setWRW = function(wrw) {
			selectedWRW = wrw;
		};
		services.getWRW = function() {
			return selectedWRW;
		};

		services.reset = function() {
			selectedXZQ = undefined;
			selectedWRW = undefined;
			changedCallBack = undefined;
		}

		return services;
	} ]);

}(window, angular));